package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasCoUserEntity;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasUserEntity;
import com.xbongbong.paas.help.subform.abstracts.AbstractUserTeamTransferSubForm;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.subform.PaasTeamEnum;
import org.springframework.stereotype.Component;

/**
 * @author: 汪腾伟
 * @date: Created in 2021/12/16 19:57
 * @description： paas团队转化（paas的团队目前主要是协同人）
 */
@Component
public class PaasTeamTransferSubForm extends AbstractUserTeamTransferSubForm {

    @Override
    public PaasFormDataEntity transferTeamToFormData(UserTeamEntity userTeamEntity) {
        PaasUserEntity paasUserEntity = new PaasUserEntity();
        BeanUtil.copyProperties(userTeamEntity, paasUserEntity);
        PaasFormDataEntity paasFormDataEntity = createPaasFormDataEntityForUserTeam(userTeamEntity);

        JSONObject teamJson = JSONObject.parseObject(JSON.toJSONString(paasUserEntity));

        JSONObject data = new JSONObject();

        PaasTeamEnum[] paasTeamEnums = PaasTeamEnum.values();
        for (PaasTeamEnum paasTeamEnum : paasTeamEnums) {
            String dbAttr = paasTeamEnum.getDbAttr();
            String dataAttr = paasTeamEnum.getDataAttr();
            Integer fieldType = paasTeamEnum.getFieldType();
            formatDataForEs(teamJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    @Override
    public UserTeamEntity transferFormDataToTeam(PaasFormDataEntity paasFormDataEntity) {
        return null;
    }
}
